package com.fruit.service.redis.impl;

import java.util.Map;
import java.util.Random;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.constants.RedisConstant;
import com.fruit.service.redis.JedisUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class JedisService implements JedisUtil{

	protected Logger logger = LoggerFactory.getLogger(JedisUtil.class);
	
/*	@Autowired
    private JedisCluster jedisCluster;*/
	
	@Autowired
    private JedisPool jedisPool;//注入JedisPool
	
	@PostConstruct
	public void init(){
		System.out.println(jedisPool.getResource());
	}
	
	@Override
	public boolean lock(String key, long timeout, int expire) {
		Jedis jedis = getRedis();
		long nanoTime = System.nanoTime();
		timeout = nanoTime + timeout * RedisConstant.MILLI_NANO_TIME;
		try{
			
			while(System.nanoTime() < timeout){
				if(jedis.setnx(key, key) == 1){
					jedis.expire(key, expire);
					return true;
				}
				logger.info("出现锁等待:"+key);
				Thread.sleep(3, new Random().nextInt(30));
			}
			return false;
		}catch(Exception e){
			logger.info("获取锁错误"+key);
			return false;
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
	}

	@Override
	public void unlock(String key) {
		Jedis jedis = null;
		try{
			jedis = getRedis();
			jedis.del(key);
		}catch(Exception e){
			logger.info("删除锁错误"+key);
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
		
	}

	@Override
	public String get(String key){
		Jedis jedis = null;
		try{
			jedis = getRedis();
			return jedis.get(key);
		}catch(Exception e){
			logger.info("获取redis值"+key);
			return null;
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
		
	}

	@Override
	public void set(String key, String value) {
		Jedis jedis = null;
		try{
			jedis = getRedis();
			jedis.set(key, value);
		}catch(Exception e){
			logger.info("设置redis值"+key);
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}	
	}

	@Override
	public void set(String key, String value, int expire) {
		
		Jedis jedis = null;
		try{
			jedis = getRedis();
			jedis.set(key, value);
			jedis.expire(key, expire);
		}catch(Exception e){
			logger.info("设置redis值"+key);
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}	
	}

	@Override
	public Jedis getRedis() {
		return jedisPool.getResource();
	}

	@Override
	public void del(String key) {
		
		Jedis jedis = null;
		try{
			jedis = getRedis();
			getRedis().del(key);
		}catch(Exception e){
			logger.info("删除redis值"+key);
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}	
		
	}

	@Override
	public void expire(String key, int expire) {
		Jedis jedis = null;
		try{
			jedis = getRedis();
			jedis.expire(key, expire);
		}catch(Exception e){
			logger.info("删除redis值"+key);
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
	}

	@Override
	public boolean exists(String key) throws Exception {
		// TODO Auto-generated method stub
		Jedis jedis = null;
		try{
			jedis = getRedis();
			return jedis.exists(key);
		}catch(Exception e){
			logger.info("删除redis值"+key);
			throw new Exception(e);
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
	}

	@Override
	public Map<String, String> hgetAll(String key){
		Jedis jedis = null;
		try{
			jedis = getRedis();
			return jedis.hgetAll(key);
		}catch(Exception e){
			logger.info("获取HMAP集合"+key);
			return null;
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
	}

	@Override
	public Long hdel(String key, String... fields) {
		Jedis jedis = null;
		try{
			jedis = getRedis();
			return jedis.hdel(key,fields);
		}catch(Exception e){
			logger.info("删除MAP中的子值"+key);
			return null;
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
	}

	@Override
	public Long hset(String key, String field, String value) {
		Jedis jedis = null;
		try{
			jedis = getRedis();
			return jedis.hset(key,field,value);
		}catch(Exception e){
			logger.info("设置值HMAP"+key);
			return null;
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
	}
	
}
